package com.jinzhi.jzweb.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.jzcultivate.controller.CourseController;
import com.jinzhi.jzweb.pojo.entity.ChapterDocument;
import com.jinzhi.jzweb.service.ChapterDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 课程的定时器
 */
@Component
public class CourseTask {

    @Autowired
    private ChapterDocumentService chapterDocumentService;

    @Autowired
    private CourseController courseController;

    /**
     * 每天凌晨2点执行一次 将文档转成pdf格式  (由于需要调用CourseController的方法 ，故逻辑写在此)
     */
//    @Scheduled(cron = "0 0 2 * * ?")
    @Scheduled(fixedRate = 7200000) // 1小时 = 3600000毫秒  1小时执行一次
    public void fileToPdfTask(){

        //查询剩余未被转换成pdf的文档资源
        Wrapper<ChapterDocument> wrapper = new EntityWrapper<ChapterDocument>();
        wrapper.eq("type", 2);
        wrapper.isNull("file_pdf").or("file_pdf","");
        List<ChapterDocument> chapterDocuments = chapterDocumentService.selectList(wrapper);
        //这里循环去调用 文档转pdf预览的接口
        if(null!=chapterDocuments && !chapterDocuments.isEmpty()){
            for (ChapterDocument chapterDocument : chapterDocuments) {
                courseController.documentConversion(String.valueOf(chapterDocument.getId()));
            }
        }

    }


}
